package com.nineclock.message.listener;

import com.alibaba.fastjson.JSON;
import com.nineclock.common.constant.MessageConstant;
import com.nineclock.message.jpush.JPushManager;

import com.nineclock.system.pojo.EntryAudit;
import com.nineclock.system.pojo.NcMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @title：
 * @author: Hugh
 * @date: 2022年03月22日 12:51
 * @description:
 */
@Component
public class AuditListener {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private JPushManager jPushManager;
    @RabbitListener(queues = MessageConstant.AUDIT)
    public void listenSimpleQueueMessage(String msg) {
        NcMessage ncMessage = JSON.parseObject(msg, NcMessage.class);
        System.out.println(ncMessage);
        mongoTemplate.save(ncMessage);
        Map<String, String> map = new HashMap<>();
        map.put("入职信息", "入职信息");
        try {
            jPushManager.sendNotificationByAlias(ncMessage.getTitle(), ncMessage.getContent(), ncMessage.getTargets(),map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = MessageConstant.AUDIT_NOTICES)
    public void listenAuditNoticesMessage(String msg) {
        EntryAudit entryAudit = JSON.parseObject(msg, EntryAudit.class);
        NcMessage ncMessage = mongoTemplate.findById(entryAudit.getNotifyMsgId(), NcMessage.class);

        Map<String, String> map = new HashMap<>();
        boolean approved = entryAudit.isApproved();
        if (approved) {
            mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(entryAudit.getNotifyMsgId())),
                    new Update().set("title", "入职申请结果").set("content", "审核通过").set("useStatus", 1)
                            .set("approveStatus", 1).set("approveResult", "1")
                    , NcMessage.class);


            try {
                jPushManager.sendNotificationByAlias(ncMessage.getTitle(), ncMessage.getContent(), entryAudit.getMobileList(), map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {

            mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(entryAudit.getNotifyMsgId())),
                    new Update().set("title", "入职申请结果").set("content", "未通过").set("useStatus", 1)
                            .set("approveStatus", 1).set("approveResult", "0")
                    , NcMessage.class);

            try {
                jPushManager.sendNotificationByAlias(ncMessage.getTitle(), ncMessage.getContent(), entryAudit.getMobileList(),map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


}
